﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class Default : Telerik.Web.UI.RadAjaxPage 
{
    private bool ValidateData()
    {
        if (string.IsNullOrEmpty(txtUsername.Text))  return false;
        if (string.IsNullOrEmpty(txtPassword.Text)) return false;

        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        try
        {
            if (ValidateData())
            {
                GPSTrackingServerDB.Entities.Users userEntity = GPSTrackingServerDB.Data.DataRepository.UsersProvider.GetByUsername(txtUsername.Text);
                if (userEntity != null)
                {
                    if (userEntity.IsActive)
                    {
                        if (userEntity.Password == txtPassword.Text)
                            System.Web.Security.FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
                        else
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowError", "show_Error('La contraseña no es correcta','Login ...');", true);
                    }
                    else
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowError", "show_Error('El usuario esta desactivado','Login ...');", true);

                }
                else
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorEntity", "show_Error('No existe el usuario en la base de datos','Login ...');", true);
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('"+ex.Message+"','Login error ...');", true);
        }
    }
}
